<?php
//$Id: admin.php 181 2012-02-27 05:32:22Z tomqin@gmail.com $

/**
 * AdminController 后台管理基类
 *
 * @copyright		Copyright (C) 2010-2012 ARESCMS Inc.
 * @author			TomQin <tomqin@gmail.com>
 * @license			http://www.arescms.cn/license/
 * @package Core
 */

define('IN_ADMIN', true);

class AdminController {

	public $cityid = 0;
	public $user = null;
	public $controller = '';
	public $action = '';
	public $menu = 0;

	//@overwrite
	function init(){}

	//调用
	function initialize($controller, $action){
		$this->cityid     = Base::getCityId();
		$this->user	      = new AdminUser();
		$this->action	  = $action;
		$this->controller = $controller;

		switch($this->controller){
			case 'zone':
				$menu = 2;
			break;
			case 'store':
			case 'dish':
				$menu = 1;
			break;
			default:
				$menu = 0;
		}

		$this->setVar('admin', $this->user);
		$this->setVar('__tpl_menu', $menu);
		$this->init();
	}

	function setVar($key, $var){
		View::setVar($key, $var);
	}

	function render($tpl){
		View::setVar('__tpl_render', $tpl);
		View::addView('admin/default');
		View::display();
	}

	function renderDialog($message, $jumpurl = 'back'){
		if(!is_array($jumpurl) || empty($jumpurl)){
			if($jumpurl == 'back' || empty($jumpurl)){
				$jumpurl = 'javascript:history.go(-1);';
			}else{
				$jumpurl = "location.href='".$jumpurl."'";
			}
		}
		$this->setVar('__tpl_message', $message);
		$this->setVar('__tpl_jumpurl', ($jumpurl == 'back' || empty($jumpurl)) ? 'javascript:history.go(-1);' : $jumpurl);
		$this->render('admin/common_dialog', '提示信息');
	}

	static function run(){
		$con = self::getController();
		$act = self::getAction();

		if($con == 'login'){
			self::login($act);
			exit;
		}

		$con_file  = ARESCMS_PATH . 'admin' . DS . $con . '.php';
		$con_class = "{$con}_controller";
		if(file_exists($con_file)){
			require_once $con_file;

			$control  = new $con_class;
			$control->initialize($con, $act);
			$method   = "on".$control->action;
			if(method_exists($control, $method)){
				call_user_func(array($control, $method));
			}else{
				exit('The action `' .$act. '` is not exists!');
			}
		}else{
			exit('Admin controller `' .$con. '` is not exists!');
		}
	}

	static function login($act = ''){
		if(array_get_string($_POST, 'dosubmit')){
			$username = array_get_string($_POST, 'username');
			$password = array_get_string($_POST, 'password');
			$res = AdminUser::login($username, $password);

			if($res){
				header("Location: /admin/index.php");
				exit;
			}else{
				View::setVar('error', '用户名或者密码错误！');
			}
		}

		if($act == 'logout'){
			AdminUser::setAdmin(0);
			View::setVar('error', '成功退出！');
		}

		View::addView('admin/login');
		View::display();
	}

	static function getAction(){
		if(isset($_GET['a']))
			return strtolower($_GET['a']);
		else
			return 'index';
	}

	static function getController(){
		if(isset($_GET['c']))
			return strtolower($_GET['c']);
		else
			return 'main';
	}

}

class AdminUser{
	var $userId;
	var $userName;

	function __construct(){
		$admin = self::getAdmin();
		if(isset($admin['userId']) && isset($admin['userId'])){
			$this->userId   = $admin['userId'];
			$this->userName = $admin['userName'];
			$this->isAdmin = true;
		}else{
			$this->userId   = 0;
			$this->userName = '';
			$this->isAdmin = false;
		}
	}

	function getId(){
		return $this->userId;
	}

	function getName(){
		return $this->userId;
	}

	function isAdmin(){
		return $this->isAdmin;
	}

	function isLogin(){
		return $this->userId && $this->isAdmin;
	}

	static function getAdmin(){
		return $_SESSION['admin'];
	}

	static function setAdmin($admin_id, $admin_name = '', $admin_permit = null){
		$admin_id = intval($admin_id);
		$admin_name = trim($admin_name);
		if($admin_id){
			$_SESSION['admin'] = array(
				'userId' => $admin_id,
				'userName' => $admin_name,
				'userPermit' => $admin_permit,
			);
			DB::prepare('update `t_admin` set `login_dt` = Now(), `login_ip` = ? where aid = ?')
					->execute(array(ip2long(IP_ADDRESS), $admin_id));
		}else{
			unset($_SESSION['admin']);
		}
	}

	static function checkLogin(){
		$admin = self::getAdmin();
		return ($admin && $admin['userId']);
	}

	static function login($username, $password){
		$admin = DB::getOne("select * from `t_admin` where `username` like ?", array($username));
		if($admin['aid'] && $admin['password'] == md5($admin['salt'].md5($password))){
			self::setAdmin($admin['aid'], $admin['username'], null);
			return true;
		}else{
			self::setAdmin(0);
			return false;
		}
	}
}
?>